<template>
  <div>
    <!-- 添加/修改弹出框 -->
    <el-dialog
      :title="dialogFormTitle"
      visible.sync="dialogFormVisible"
      @close="closedialog"
    >
      <el-form
        ref="employee"
        :rules="rules"
        :model="employeemodify"
        label-width="80px"
        style="width: 300px"
      >
        <el-form-item label="姓名" prop="ename">
          <el-input v-model="employeemodify.ename"></el-input>
        </el-form-item>

        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="employeemodify.gender">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="部门" prop="department">
          <el-select
            v-model="employeemodify.department.did"
            placeholder="请选择部门"
          >
            <el-option
              v-for="item in departments"
              :key="item.value"
              :label="item.dname"
              :value="item.did"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="生日" prop="birthdate">
          <div class="block">
            <el-date-picker
              v-model="employeemodify.birthdate"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </div>
        </el-form-item>

        <el-form-item
          label="邮箱"
          v-if="this.dialogFormTitle == '添加员工'"
          prop="email"
        >
          <el-input v-model="employeemodify.email"></el-input>
        </el-form-item>
        
      </el-form>
      
    </el-dialog>

    <!-- 搜索表单 -->
    <el-form ref="employee" :model="employee" label-width="80px" :inline="true">
      <el-form-item>
        <el-button type="primary" @click="batchDelete">批量删除</el-button>
    </el-form-item>

      <el-form-item label="姓名">
        <el-input v-model="employee.ename"></el-input>
      </el-form-item>

      <el-form-item label="性别">
        <el-radio-group v-model="employee.gender">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="部门">
        <el-select v-model="employee.department.did" placeholder="请选择部门">
          <el-option
            v-for="item in departments"
            :key="item.value"
            :label="item.dname"
            :value="item.did"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询所有信息</el-button>
        <el-button @click="add">添加</el-button>
        <el-button @click="handAdd">添加/修改</el-button>
      </el-form-item>
    </el-form>

    <!-- 展示数据 -->
    <el-table
      :data="pageBean.beanList"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="eid" label="员工编号" width="120" align="center">
      </el-table-column>
      <el-table-column prop="ename" label="员工姓名" width="120" align="center">
      </el-table-column>
      <el-table-column prop="password" label="密码" width="220" align="center">
      </el-table-column>
      <el-table-column prop="gender" label="性别" width="50" align="center">
      </el-table-column>
      <el-table-column prop="birthdate" label="生日" width="120" align="center">
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="120" align="center">
      </el-table-column>
      <el-table-column
        prop="department.dname"
        label="部门名称"
        width="180"
        align="center"
      >
      </el-table-column>
      
    </el-table>

    <!-- 展示页码 -->
    <div class="block">
      <span class="demonstration"></span>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[1, 2, 3]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageBean.totalRecord"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    var checkemail = (rule, value, callback) => {
      if (value === undefined || value === "") {
        callback(new Error("填写邮箱"));
      } else if (!/^[\w.-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/.test(value)) {
        callback(new Error("邮箱格式错误"));
      } else {
        this.$axios.post("/employee/emailUsed/" + value).then((resp) => {
          if (resp.data.data) {
            callback(new Error("邮箱已被使用"));
          } else {
            callback();
          }
        });
      }
    };
    return {
      form: {
        name: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
      dialogTableVisible: false,
      dialogFormVisible: false,
      multipleSelection: [],
      dialogFormTitle: "",
      pageIndex: 1,
      pageSize: 2,
      rules: {
        ename: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          { min: 2, max: 7, message: "长度在 2 到 7 个字符", trigger: "blur" },
        ],

        gender: [{ required: true, message: "请选择性别", trigger: "change" }],
        birthdate: [
          {
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],

        department: [
          { required: true, message: "请选择部门", trigger: "change" },
        ],

        email: [{ validator: checkemail, trigger: "blur" }],
      },
      employee: {
        department: {
          did: null,
        },
      },
      employeemodify: {
        department: {
          did: null,
        },
      },
      pageBean: {},
      departments: {},
    };
  },
  mounted() {
    this.getAll();
    this.getAllDepart();
  },
  methods: {
    //  批量删除
    batchDelete(index, row) {
      if(this.multipleSelection.length>0){
        let myeids =[]
        for(let i=0;i<this.multipleSelection.length;i++){
          myeids.push(this.multipleSelection[i].eid)
        }
        //  get请求
        // let myeids ="?"
        // for(let i=0;i<this.multipleSelection.length;i++){
        //     myeids += "eids="+this.multipleSelection[i].eid+"&";
        // }


        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            // axios.get("/employee/deletebatchget"+myeids)
            //  axios.post("/employee/deletebatchget",myeids)
            axios.post("/employee/deletebatchget",{"eids":myeids})
            .then((resp) => {
              if (resp.data.code == 200) {
                this.$message("删除成功");
                this.getAll();
              } else {
                this.$message.error("删除失败");
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      }else{
        this.$message.error("请选择要删除的数据");
      }
    },  
    //  复选框选中
    handleSelectionChange(val) {
        this.multipleSelection = val;
      },
    //  关闭弹出框
    closedialog() {
      //  情况数据
      this.employeemodify = { department: { did: null } };
    },
    //  添加弹出框
    handAdd() {
      (this.dialogFormVisible = true), (this.dialogFormTitle = "添加员工");
    },
    //  修改弹出框
    handEdit2(index, row) {
      (this.dialogFormVisible = true),
        (this.dialogFormTitle = "修改员工"),
        (this.employeemodify = { ...row });
    },
    //  修改或添加
    handEditAdd() {
      if (this.dialogFormTitle === "修改员工") {
        axios.post("/employee/modify", this.employeemodify).then((resp) => {
          if (resp.data.code == 200) {
            this.$message("修改成功");
          } else {
            this.$message.error(JSON.stringify(resp.data.msg));
          }
        });
        this.dialogFormVisible = false;
      } else {
        axios.post("/employee/add", this.employeemodify).then((resp) => {
          if (resp.data.code == 200) {
            this.$message("添加成功");
          } else {
            this.$message.error(JSON.stringify(resp.data.msg));
          }
        });
        this.dialogFormVisible = false;
      }
    },
    //  编辑
    handleEdit(index, row) {
      this.$router.push("/employeemodify/" + row.eid);
    },

    //  删除
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          axios.post("/employee/delete", row).then((resp) => {
            if (resp.data.code == 200) {
              this.$message("删除成功");
              this.getAll;
            } else {
              this.$message.error("删除失败");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //  查询按钮
    onSubmit() {
      console.log(this.employee);
      (this.pageIndex = 1), (this.pageSize = 2);
      this.getAll();
    },
    // 查询所有部门
    getAllDepart() {
      axios.post("/department/findAll").then((resp) => {
        this.departments = resp.data.data;
      });
    },

    // 查询所有信息
    getAll() {
      getAllEmployees(this.pageIndex,this.pageSize,this.employee).then((resp) => {
          if(resp.data.code==200){
            this.pageBean = resp.data.data;
          }
        });
    },

    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      //  重新查询
      this.pageSize = val;
      this.getAll();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      //  重新查询
      this.pageIndex = val;
      this.getAll();
    },

    //  添加页面
    add() {
      this.$router.push("/employeeadd");
    },
  },
};
</script>

<style>
</style>